package com.sulent.frame.web.controller.test;

import com.sulent.frame.web.utils.RedLockUtil;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedisController
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/2/26 11:41
 * @Version 1.0
 */

@RequestMapping("/redis")
@RestController
public class RedisController {

    Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

//    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/string")
    public void string(){
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set("spring-redis-string","string-value");
    }

    @GetMapping("/hash")
    public void hash(){
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("spring-redis-hash","hash-key","hash-value");
    }

    @GetMapping("/list")
    public void list(){
        ListOperations<String, String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("spring-redis-list", "list-value");
    }

    @GetMapping("/set")
    public void set(){
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        setOperations.add("spring-redis-set","set-v1","set-v2");
    }

    @GetMapping("/zset")
    public void zset(){
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("spring-redis-zSet","z1",80.00);
        zSetOperations.add("spring-redis-zSet","z2",90.00);
    }

    @Autowired
    private RedLockUtil redLockUtil;

    @GetMapping("/lock")
    public void lock() throws InterruptedException {

        redLockUtil.lock("sub-redis-lock-1",60*1000);//锁60秒后自动释放

        String name = Thread.currentThread().getName();
        logger.info("线程：{}，抢到锁了，开始处理业务",name);
        Thread.sleep(30 * 1000); // 模拟业务处理耗时30s
        logger.info("线程：{}，业务处理结束，释放锁了",name);

        redLockUtil.unLock("sub-redis-lock-1");



    }

    @GetMapping("/unlock")
    public void unlock(){
        redLockUtil.unLock("Redis-Lock-Test_1");
    }


    /**
     * Reids用法之生成编码
     * 规则：编码前缀++日期（yyyyMMdd）+0001（递增）
     */
    @GetMapping("/generateCode")
    public String generateCode() {
        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String redisKey = "GCPrefix-" + dateStr;
        // 获取并递增计数器
        Long counter = redisTemplate.opsForValue().increment(redisKey);
        if ( 1 == counter) {
            redisTemplate.expire(redisKey, 86400, TimeUnit.SECONDS);
        }
        return redisKey + String.format("%04d", counter);
    }





}
